﻿using MediatR;
using Traceability.API.Applications.Command.AquacultureCommand;
using Traceability.Domain;
using Traceability.ErrorCode;
using Traceability.Infrastructure;

namespace Traceability.API.Applications.CommandHandler.AquacultureCommandHandler
{
    public class HealthcareDelCommandHandler : IRequestHandler<HealthcareDelCommand, APIResult<int>>
    {
        private readonly IBaseRepository<Healthcare_Information> _healthcareRepository;

        public HealthcareDelCommandHandler(IBaseRepository<Healthcare_Information> healthcareRepository)
        {
            _healthcareRepository = healthcareRepository;
        }

        public async Task<APIResult<int>> Handle(HealthcareDelCommand request, CancellationToken cancellationToken)
        {
            var res = _healthcareRepository.GetAll().FirstOrDefault(s => s.Id == request.Id)!;
            res.IsDeltede=true;
            int count= await _healthcareRepository.Update(res);
            APIResult<int> aPIResult = new APIResult<int>();
            if (count > 0)
            {
                aPIResult.Code = EnumCode.Ok;
                aPIResult.Msg = "删除成功";
                aPIResult.Data = count;
            }
            else
            {
                aPIResult.Code = EnumCode.File;
                aPIResult.Msg = "删除失败";
                aPIResult.Data = count;
            }
            return await Task.FromResult(aPIResult);
        }
    }
}
